package com.tjsinfo.tjsoa.worklog.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.common.PageController;
import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.User;
import com.tjsinfo.tjsoa.worklog.bo.IWorkLogMainBo;
import com.tjsinfo.tjsoa.worklog.dao.IWorkLogMainDao;
import com.tjsinfo.tjsoa.worklog.vo.WorkLogMain;

public class WorkLoglistmodeAction extends Action{
	private IWorkLogMainDao workLogMainDao;
	

	public IWorkLogMainDao getWorkLogMainDao() {
		return workLogMainDao;
	}


	public void setWorkLogMainDao(IWorkLogMainDao workLogMainDao) {
		this.workLogMainDao = workLogMainDao;
	}


	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		WorkLogMain workLogMain = new WorkLogMain();
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(user.getUser_id());
		workLogMain.setTjsUser(tjsUser);
		workLogMain.setType(request.getParameter("type"));
//		session.setAttribute("type", "L"+request.getParameter("type"));
	List list =	workLogMainDao.selectWorkLogMainUserAndType(workLogMain);
//		Integer userid = SessionUser.getUserId(request);
//		String type = request.getParameter("type");
//		String connect  = request.getParameter("keyword");
//		List list = null;
//		if (type == null || "".equals(type)) {
//			list = workLogMainBo.selectLike(userid, connect);
//		} else {
//			list =  workLogMainBo.selectLikeType(userid, type, connect);
//		}
		Integer contentspage = null;
		String start = null;
		 PageController pageController = new PageController();
		    if(request.getParameter("page") != null && request.getParameter("start") == null )
			{
				pageController.setPageController(list);
				pageController.setMaxPageRows(18);
				list = (ArrayList) pageController.getPageContents(Integer.parseInt(request.getParameter("page") ));
		
			}
		    else if (request.getParameter("page") != null) {
				contentspage = Integer.parseInt(request.getParameter("page"));
				start = request.getParameter("start");
				pageController.setPageController(list);
				pageController.setMaxPageRows(18);
				if (start.equals("up")) {
					list = (ArrayList) pageController
							.getPageContents(contentspage - 1);
				} else if(start.equals("down")) {
					list = (ArrayList) pageController
							.getPageContents(contentspage + 1);
				}
				else if(start.equals("first"))
				{
					list = (ArrayList) pageController
					.getPageContents(1);
				}
				else if(start.equals("last"))
				{
					list = (ArrayList) pageController
					.getPageContents(pageController.getPageCount());
				}
			} else {
				pageController.setPageController(list);
				pageController.setMaxPageRows(18);
				list = (ArrayList) pageController.getPageContents(1);
			}
		request.setAttribute("list", list);
		request.setAttribute("type",request.getParameter("type"));
		request.setAttribute("pageController",pageController);
	/*	request.setAttribute("keyword", connect);
		request.setAttribute("type", type);*/
		return mapping.findForward("ok");
	}
	
}
